package src;

import javax.swing.*;
import java.awt.*;
import java.awt.Window;
import java.sql.*;

public class PaymentAddDialog extends AddDialog {
    public PaymentAddDialog(Window parent, Connection connection) {
        super(parent, "添加收费信息", connection);
        
        // 自动生成收费ID
        String nextPaymentId = generateNextPaymentId();
        JTextField paymentIdField = new JTextField(nextPaymentId);
        paymentIdField.setEditable(false);
        addInputField("ID:", paymentIdField);
        
        addInputField("账单ID:", new JTextField());
        addInputField("支付日期:", new JTextField());
        addInputField("金额:", new JTextField());
        
        // 添加支付方式下拉框
        JComboBox<String> paymentMethodCombo = new JComboBox<>(new String[]{"微信", "支付宝", "银行卡", "现金"});
        addComboBox("支付方式:", paymentMethodCombo);
        
        // 添加业务员下拉框
        JComboBox<String> salesmanCombo = new JComboBox<>();
        salesmanCombo.addItem(null);  // 添加一个空选项
        try {
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT salesman_id, name FROM salesman");
            while (rs.next()) {
                String salesmanId = rs.getString("salesman_id");
                String salesmanName = rs.getString("name");
                salesmanCombo.addItem(salesmanName + " (" + salesmanId + ")");
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, "加载业务员信息失败: " + e.getMessage());
        }
        addComboBox("业务员:", salesmanCombo);

        confirmButton.addActionListener(e -> {
            try {
                // 从业务员选择框中提取业务员ID
                String selectedSalesman = salesmanCombo.getSelectedItem().toString();
                String salesmanId = selectedSalesman.substring(selectedSalesman.lastIndexOf("(") + 1, selectedSalesman.lastIndexOf(")"));

                String sql = "INSERT INTO payment (payment_id, bill_id, payment_date, amount, payment_method, salesman_id) VALUES (?, ?, ?, ?, ?, ?)";
                PreparedStatement pstmt = connection.prepareStatement(sql);
                pstmt.setString(1, paymentIdField.getText());
                pstmt.setString(2, textFields.get("账单ID:").getText());
                pstmt.setString(3, textFields.get("支付日期:").getText());
                pstmt.setDouble(4, Double.parseDouble(textFields.get("金额:").getText()));
                pstmt.setString(5, paymentMethodCombo.getSelectedItem().toString());
                pstmt.setString(6, salesmanId);
                
                pstmt.executeUpdate();
                JOptionPane.showMessageDialog(this, "收费信息添加成功！");
                dispose();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "添加收费信息失败: " + ex.getMessage());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "金额必须是数字！");
            }
        });
    }

    private String generateNextPaymentId() {
        try {
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT payment_id FROM payment ORDER BY payment_id");
            
            if (!rs.next()) {
                return "PAY000001";
            }
            
            java.util.Set<Integer> usedIds = new java.util.HashSet<>();
            do {
                String currentId = rs.getString("payment_id");
                int num = Integer.parseInt(currentId.substring(3));
                usedIds.add(num);
            } while (rs.next());
            
            int nextNum = 1;
            while (usedIds.contains(nextNum)) {
                nextNum++;
            }
            
            return String.format("PAY%06d", nextNum);
            
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, "生成收费ID失败: " + e.getMessage());
            return "PAY000001";
        }
    }
} 